package com.lele.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.lele.enums.RetCode;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: lele
 * @Description: 定义统一返回结果对象
 */
public class Result<T> implements Serializable {
    private int code;

    private String message;

    private T data;

    private boolean success;

    /**
     * 正常
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> OK() {
        return new Result<T>().setCode(RetCode.OK.getRetCode()).setMessage(RetCode.OK.getRetMsg());
    }

    public static <T> Result<T> OK(T data) {
        return new Result<T>().setCode(RetCode.OK.getRetCode()).setMessage(RetCode.OK.getRetMsg()).setData(data);
    }

    public static <T> Result<T> OK(String msg, T data) {
        return new Result<T>().setCode(RetCode.OK.getRetCode()).setMessage(msg).setData(data);
    }

    /**
     * 失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> FAIL() {
        return new Result<T>().setCode(RetCode.FAIL.getRetCode()).setMessage(RetCode.FAIL.getRetMsg());
    }

    public static <T> Result<T> FAIL(String msg) {
        return new Result<T>().setCode(RetCode.FAIL.getRetCode()).setMessage(msg);
    }

    public static <T> Result<T> FAIL(int code, String msg) {
        return new Result<T>().setCode(code).setMessage(msg);
    }

    /**
     * 系统错误或异常
     * @param <T>
     * @return
     */
    public static <T> Result<T> ERROR() {
        return new Result<T>().setCode(RetCode.ERROR.getRetCode()).setMessage(RetCode.ERROR.getRetMsg());
    }

    public static <T> Result<T> ERROR(String msg) {
        return new Result<T>().setCode(RetCode.ERROR.getRetCode()).setMessage(msg);
    }

    public static <T> Result<T> ERROR(int code, String msg) {
        return new Result<T>().setCode(code).setMessage(msg);
    }

    @JsonIgnore
    protected boolean notEmpty() {
        return !isEmpty();
    }

    @JsonIgnore
    protected boolean isEmpty() {
        if (this.data == null) {
            return true;
        }

        if (this.data instanceof CharSequence) {
            return ((CharSequence) this.data).length() == 0;
        }

        if (this.data instanceof Collection) {
            return ((Collection) this.data).isEmpty();
        }

        if (this.data instanceof Map) {
            return ((Map) this.data).isEmpty();
        }

        if (this.data instanceof Object[]) {
            Object[] object = (Object[]) this.data;
            if (object.length == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 提供简洁判定处理是否成功
     *
     * @return
     */
    public boolean isSuccess() {
        return success;
    }

    private void setSuccess(boolean success) {
        this.success = success;
    }
    public int getCode() {
        return code;
    }

    public Result<T> setCode(int code) {
        this.code = code;
        setSuccess(code == RetCode.OK.getRetCode());
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 转化为JSON字符串
     */
    public String toJsonString() {
        return JSON.toJSONString(this);
    }
}
